package com.example.photoview;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Model {//规范字节和时间格式
    public static double KB=1024.0;
    public static double MB=1024.0*1024.0;
    public static double GB=1024.0*1024.0*1024.0;
    public static String getFormLength(long filelength){
    String standard=null;
    if(filelength<KB){
        standard=String.format("%d Byte",filelength);}
       else if(filelength<MB){
            standard=String.format("%.0f KB",filelength/KB);
         }
       else if(filelength<GB){
        standard=String.format("%.0f MB",filelength/MB);
       }
        else{
         standard=String.format("%.0f GB",filelength/GB);
        }
    return standard;
    }
    public static String getFormDate(long fileDate){
        Date date=new Date(fileDate);
        SimpleDateFormat slf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return slf.format(date);
    }
    static byte[] getByteByFile(File file){
        try(FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024)){
            byte[] bytes = new byte[1024];
            int i;
            while ((i = fis.read(bytes)) != -1) {
                bos.write(bytes, 0, i);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    static boolean getFileByByte(byte[] bytes, File file) {
        try (FileOutputStream fos = new FileOutputStream(file);
             BufferedOutputStream bos = new BufferedOutputStream(fos)){
            bos.write(bytes);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
